// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SwingUtils.java

package com.succez.custom.gssjxt.client;

import java.awt.*;
import java.io.File;
import java.io.PrintStream;
import javax.swing.*;

// Referenced classes of package com.succez.custom.gssjxt.client:
//            _FileFilter

public class SwingUtils
{

    private SwingUtils()
    {
    }

    public static final void setUIFont()
    {
        setUIFont("Simsun", 12);
    }

    public static final void setUIFont(String fontname, int size)
    {
        Font font = new Font(fontname, 0, size);
        UIManager.put("Button.font", font);
        UIManager.put("ToggleButton.font", font);
        UIManager.put("RadioButton.font", font);
        UIManager.put("CheckBox.font", font);
        UIManager.put("ColorChooser.font", font);
        UIManager.put("ToggleButton.font", font);
        UIManager.put("ComboBox.font", font);
        UIManager.put("ComboBoxItem.font", font);
        UIManager.put("InternalFrame.titleFont", font);
        UIManager.put("Label.font", font);
        UIManager.put("List.font", font);
        UIManager.put("MenuBar.font", font);
        UIManager.put("Menu.font", font);
        UIManager.put("MenuItem.font", font);
        UIManager.put("RadioButtonMenuItem.font", font);
        UIManager.put("CheckBoxMenuItem.font", font);
        UIManager.put("PopupMenu.font", font);
        UIManager.put("OptionPane.font", font);
        UIManager.put("Panel.font", font);
        UIManager.put("ProgressBar.font", font);
        UIManager.put("ScrollPane.font", font);
        UIManager.put("Viewport", font);
        UIManager.put("TabbedPane.font", font);
        UIManager.put("TableHeader.font", font);
        UIManager.put("TextField.font", font);
        UIManager.put("PasswordFiled.font", font);
        UIManager.put("TextArea.font", font);
        UIManager.put("TextPane.font", font);
        UIManager.put("EditorPane.font", font);
        UIManager.put("TitledBorder.font", font);
        UIManager.put("ToolBar.font", font);
        UIManager.put("ToolTip.font", font);
        UIManager.put("Tree.font", font);
    }

    public static final String selectDir(Component parent, String caption, String initdir)
    {
        File files[] = showFileChooserDialog(parent, false, true, caption, null, initdir, null);
        return files != null && files.length != 0 ? files[0].getPath() : null;
    }

    public static final String getSaveFilename(Component parent, String caption, String filter, String initdir, String defFileExt, String initFilename)
    {
        File files[] = showFileChooserDialog(parent, true, false, caption, filter, initdir, initFilename);
        if(files == null || files.length == 0)
            return null;
        String fn = files[0].getPath();
        if(defFileExt != null)
        {
            boolean b;
            if(File.separatorChar == '/')
                b = fn.endsWith(defFileExt);
            else
                b = fn.toLowerCase().endsWith(defFileExt.toLowerCase());
            if(!b)
                fn = (new StringBuilder(String.valueOf(fn))).append(defFileExt).toString();
        }
        return fn;
    }

    public static final String getOpenFilename(Component parent, String caption, String filter, String initdir)
    {
        File files[] = showFileChooserDialog(parent, false, false, caption, filter, initdir, null);
        return files != null && files.length != 0 ? files[0].getPath() : null;
    }

    private static final void setFileChooserDialogFilter(JFileChooser jFileChooser1, String filter)
    {
        if(filter == null)
            return;
        String filters[] = filter.split("\\|");
        if(filters == null || filters.length < 2)
            return;
        jFileChooser1.setAcceptAllFileFilterUsed(false);
        jFileChooser1.resetChoosableFileFilters();
        int c = 0;
        for(int i = 0; i < filters.length; i += 2)
            try
            {
                javax.swing.filechooser.FileFilter f = new _FileFilter(filters[i + 1], filters[i]);
                jFileChooser1.addChoosableFileFilter(f);
                c++;
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }

        if(c == 0)
            jFileChooser1.setAcceptAllFileFilterUsed(true);
        else
            jFileChooser1.setFileFilter(jFileChooser1.getChoosableFileFilters()[0]);
    }

    private static final File[] showFileChooserDialog(Component parent, boolean issave, boolean selectdir, String caption, String filter, String initdir, String initFilename)
    {
        JFileChooser jFileChooser1 = new JFileChooser();
        if(caption != null)
            jFileChooser1.setDialogTitle(caption);
        if(initdir != null)
            jFileChooser1.setCurrentDirectory(new File(initdir));
        if(selectdir)
        {
            if(caption == null)
                jFileChooser1.setDialogTitle("\u9009\u62E9\u76EE\u5F55");
            jFileChooser1.setFileSelectionMode(1);
            jFileChooser1.setApproveButtonText("\u786E\u5B9A");
        } else
        {
            setFileChooserDialogFilter(jFileChooser1, filter);
        }
        int r;
        if(issave)
            r = jFileChooser1.showSaveDialog(parent);
        else
            r = jFileChooser1.showOpenDialog(parent);
        if(r == 0)
        {
            if(jFileChooser1.isMultiSelectionEnabled())
            {
                return jFileChooser1.getSelectedFiles();
            } else
            {
                File files[] = new File[1];
                files[0] = jFileChooser1.getSelectedFile();
                return files;
            }
        } else
        {
            return null;
        }
    }

    public static final void centerInScreen(Component frame)
    {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if(frameSize.height > screenSize.height)
            frameSize.height = screenSize.height;
        if(frameSize.width > screenSize.width)
            frameSize.width = screenSize.width;
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    public static final void showError(Component parent, String msg, String title)
    {
        JOptionPane.showMessageDialog(parent, msg, title != null ? title : "\u9519\u8BEF", 0);
    }

    public static final void showException(Component parent, Exception ex)
    {
        showException(parent, ex, false);
    }

    public static final void showException(Component parent, Exception ex, boolean showStack)
    {
        showError(parent, ex.getMessage(), null);
    }

    public static void main(String args[])
    {
        System.out.println(selectDir(null, "", "c:\\"));
        System.exit(0);
    }

    private static final String TITLE_ERROR = "\u9519\u8BEF";
}
